const fs = require('fs');
const shortid = require('shortid');
module.exports = agent => {

    agent.beforeStart(async function () {

        await agent.model.sync();
        await agent.spider.sync();

        console.log('===========开始初始化===========');


        agent.userDefaultLevel = 5;
        agent.pageSize = 20;

        //初始化
        const init = await agent.model.Config.get('init', false);
        if (!init) {
            //用户级别
            await agent.model.UserLevel.bulkCreate([
                {name: '系统',},
                {name: '管理员',},
                {name: '项目执行',},
                {name: '项目管理',},
                {name: '普通用户',},
            ], {updateOnDuplicate: ['name']});

            //主播级别
            await agent.model.DetailAnchorLevel.bulkCreate([
                {name: '项目执行主播', xiaohulu: false,},
                {name: '关注主播', xiaohulu: true},
                {name: '试运营素人', xiaohulu: true},
                {name: '中部主播', xiaohulu: true},
                {name: '重点主播', xiaohulu: true},
            ], {updateOnDuplicate: ['name']});

            await Promise.all([
                agent.model.User.create({username: '系统', phone: '', email: '', password: ''},
                    {hooks: false, validate: false}),
                agent.model.Message.create({title: '欢迎使用本系统', content: '欢迎'}),
                agent.model.Config.set('userAgreement', '用户协议'),
                agent.model.Config.set('userDefaultLevel', 5),
                agent.model.Config.set('screenshotToken', shortid.generate()),
                agent.model.Config.set('allowRegister', true),//开放注册
                agent.model.Platform.bulkCreate([
                    {type: 8, name: '触手', url: 'http://www.chushou.tv'},
                    {type: 6, name: '熊猫', url: 'http://www.panda.tv'},
                    {type: 1, name: '虎牙', url: 'http://www.huya.com'},
                    {type: 4, name: '龙珠', url: 'http://www.longzhu.com'},
                    {type: 21, name: '花椒', url: 'http://www.huajiao.com'},
                    {type: 15, name: '哔哩哔哩', url: 'http://live.bilibili.com'},
                    {type: 2, name: '斗鱼', url: 'http://www.douyu.com'},
                    {type: 7, name: '全民', url: 'http://www.quanmin.tv'},
                    {type: 9, name: '企鹅电竞', url: 'http://egame.qq.com'},
                    {type: 20, name: '映客'},
                    {type: 3, name: '战旗', url: 'http://www.zhanqi.com'},
                    {type: 5, name: '火猫', url: 'http://www.huomao.com'},
                ]),
                agent.model.Config.set('init', true),
            ]);
        } else {
        }

        await agent.redis.flushall();

        //检查路径
        if (!fs.existsSync(agent.config.screenshot.path))
            fs.mkdirSync(agent.config.screenshot.path);

        console.log('===========初始化完毕===========');

    });
};